# coding=utf-8

from com.android.monkeyrunner import MonkeyRunner, MonkeyImage
from com.android.monkeyrunner.easy import EasyMonkeyDevice
from com.android.monkeyrunner.easy import By
from com.android.chimpchat.hierarchyviewer import HierarchyViewer
from com.android.hierarchyviewerlib.models import ViewNode
import os, os.path, sys
sys.path.append(os.path.split(os.path.realpath(__file__))[0])
#print str(os.path.split(os.path.realpath(__file__))[0])
import resetLocation, addFriends, util;

accountMap = {}
companyMap = {}
rootPath = os.path.split(os.path.realpath(__file__))[0] + os.sep + ".." 
resPath = rootPath + os.sep + "res"
outputPath = rootPath  + os.sep + "output"  + os.sep + "image"
configPath = resPath + os.sep + "config"
imgPath = resPath + os.sep + "image"



def readConfig(accountGroup):
	#global rootPath,accountMap,companyMap
	if not os.path.exists(configPath + os.sep + "companys_accounts_" + accountGroup + ".txt"):
		util.printLog("Can't find " + "companys_accounts_" + accountGroup + ".txt")
		sys.exit(1)
	companyFile = open(configPath + os.sep + "companys_accounts_" + accountGroup + ".txt",'r')
	companyList = companyFile.readlines()
	util.printLog("Now read companys_accounts file!")
	for str in companyList:
		strList = str.split("&")
		coorDatas = []
		if len(strList) == 5:
			coorDatas.append(strList[1].strip())
			coorDatas.append(strList[2].strip())
			companyMap[strList[0].strip()] = coorDatas
			accountMap[strList[3].strip()] = strList[4].strip()
		else:
			util.printLog("Bad data : " + str)

def main():
	readConfig(sys.argv[1])
	imgMap = util.loadImage(imgPath)
	device = MonkeyRunner.waitForConnection()
	easy_device = EasyMonkeyDevice(device)
	for company in companyMap:
		coorDatas = companyMap[company]
		util.printLog("-----------------------------------------------------")
		util.printLog("Add Friends at " + company + " " + str(coorDatas))
		util.printLog("-----------------------------------------------------")
		resetLocation.resetLocation(device, easy_device, coorDatas[0], coorDatas[1], imgMap)
		for account in accountMap:
			password = accountMap[account]
			util.printLog("-----------------------------------------------------")
			util.printLog("Add Friends for " + account)
			util.printLog("-----------------------------------------------------")
			addFriends.addFriends(device, easy_device, account, password, imgMap, outputPath)
			accountMap.pop(account)
			break

if __name__ == '__main__':
	main()